package com.match.competition_role.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.match.commonutils.Result;
import com.match.competition_role.entity.CompetitionPermission;
import com.match.competition_role.entity.CompetitionRole;
import com.match.competition_role.entity.CompetitionRolePermission;
import com.match.competition_role.entity.CompetitionUserRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 芊薰
 */
@Api(tags = "权限模块")
@RestController
@RequestMapping("/admin/competition_role/competition-permission")
public class CompetitionPermissionController extends BaseController{


    @ApiOperation("新增权限")
    @PostMapping("/addPermission/{number}")
    public Result addPermission(@PathVariable(value = "number") String number,
                                @RequestBody CompetitionPermission competitionPermission){
        CompetitionUserRole competitionUserRole = competitionUserRoleService.getOne(new QueryWrapper<CompetitionUserRole>().eq("user_id", number));
        CompetitionPermission competitionPermission1=competitionPermissionService.saveCompetitionPermission(competitionPermission);
        //向中间表添加权限信息
        CompetitionRolePermission competitionRolePermission=new CompetitionRolePermission();
        competitionRolePermission.setPermissionId(competitionPermission1.getId());
        competitionRolePermission.setRoleId(competitionUserRole.getRoleId());
        competitionRolePermissionService.save(competitionRolePermission);
        return Result.ok();
    }



    @ApiOperation("查询所有权限")
    @GetMapping("/getAllPermission")
    public Result getAllPermission(){
        List<CompetitionPermission> allPermission = competitionPermissionService.findAllPermission();
        return Result.ok().data("list",allPermission);
    }



    @ApiOperation("根据权限ID删除权限")
    @DeleteMapping("/deletePermission/{permissionId}")
    public Result deletePermission(@ApiParam(required = true,value = "权限ID")
                                   @PathVariable(value = "permissionId")String permissionId){
        competitionPermissionService.deletePermissionById(permissionId);
        return Result.ok().message("删除权限成功");
    }


    @ApiOperation("根据权限ID获取权限信息")
    @GetMapping("/getPermissionInfo/{permissionId}")
    public Result getPermissionInfo(@ApiParam(required = true,value = "权限ID")
                                    @PathVariable(value = "permissionId")String permissionId){
        CompetitionPermission byId = competitionPermissionService.getById(permissionId);
        return Result.ok().data("permissionInfo",byId);
    }



    @ApiOperation("修改权限信息")
    @PutMapping("/updatePermission")
    public Result updatePermission(@RequestBody CompetitionPermission competitionPermission){
        //得到子菜单
        List<CompetitionPermission> list=competitionPermissionService.getChildren(competitionPermission.getId(),competitionPermission.getStates());
        if (list.size()>0){
            for (CompetitionPermission permission : list) {
                competitionPermissionService.updateById(permission);
            }
        }
        boolean b = competitionPermissionService.updateById(competitionPermission);
        if (!b){
            return Result.error().message("修改权限失败");
        }
        return Result.ok().message("权限修改成功");
    }



    @ApiOperation("给角色分配权限")
    @PostMapping("/roleToPermission/{roleId}")
    public Result roleToPermission(@PathVariable(value = "roleId") String roleId,
                                   @RequestBody String[] permissionId){
        //向中间表存储数据
        competitionPermissionService.saveRolePermission(roleId,permissionId);
        return Result.ok().message("权限分配成功");
    }


    @ApiOperation("根据角色ID获取权限ID")
    @GetMapping("/getAllPermissionId/{roleId}")
    public Result getAllPermissionId(@ApiParam(required = true,value = "角色ID")
                                     @PathVariable(value = "roleId")String roleId){
        List<CompetitionRolePermission> list = competitionRolePermissionService.list(new QueryWrapper<CompetitionRolePermission>().eq("role_id", roleId).select("permission_id"));
        List<String> finallyPermissionId = list.stream().map(item -> item.getPermissionId()).collect(Collectors.toList());
        return Result.ok().data("permissionIds",finallyPermissionId);
    }

}
